//
//  App.m
//  lianganyuchu
//
//  Created by iMac on 17/3/15.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "App.h"

@implementation App
+ (instancetype)shared {
    static App *_shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared = [[self alloc] init];
    });
    return _shared;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _global = [NSMutableDictionary new];
    }
    return self;
}

- (BOOL)isUserLogin {
    return self.user.access_token != nil && ![self.user.access_token isEqualToString:@""];
}

- (void)save {
    NSData *raw = [NSKeyedArchiver archivedDataWithRootObject:self.user];
    [[NSUserDefaults standardUserDefaults] setObject:raw forKey:@"user_info"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)restore {    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSData *data = [userDefault objectForKey:@"user_info"];
    self.user = [NSKeyedUnarchiver unarchiveObjectWithData:data] ;
}

- (void)logout {
    _user = nil;
    _global = nil;
    [self save];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
-(void)saveDeviceToken
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.deviceToken forKey:@"device_token"];
}
-(void)readDeviceToken
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.deviceToken=[defaults objectForKey:@"device_token"];
}
-(void)saveShopData
{
    NSData *raw = [NSKeyedArchiver archivedDataWithRootObject:self.shop];
    [[NSUserDefaults standardUserDefaults] setObject:raw forKey:@"shop_info"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)readShopData
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSData *data = [userDefault objectForKey:@"shop_info"];
    self.shop = [NSKeyedUnarchiver unarchiveObjectWithData:data] ;
}

-(void)saveOrderColor
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:@"dic.plist"];
    [_dictColor writeToFile:documnetPath atomically:YES];
}
-(void)readOrderColor
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:@"dic.plist"];
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    self.dictColor=resultDic;
}






@end
